// pages/auth/auth_phone/auth_phone.js
const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
const user = require('../../../utils/user.js');

//获取应用实例
const app = getApp()
Page({
  data: {
    // canIUse: wx.canIUse('button.open-type.getUserInfo'),
    code: '',
    checked: true,
    activeIcon: '../../../images/auth_logo/agree_2x.png',
    inactiveIcon: '../../../images/auth_logo/no_agree_2x.png',
  },

  onChange(event) {
    console.log(event);
    this.setData({
      checked: !this.data.checked
    });
  },

  onLoad: function () {
    
  },

  goBack: function () {
    wx.navigateBack({
      delta: 1,
    })
  },
  goAgreement(){
    wx.navigateTo({
      url: '/pages/agreement/agreement',
    })
  },
  goPrivacy() {
    wx.navigateTo({
      url: '/pages/Privacy/Privacy',
    })
  },
  getPhoneNumber: function (e) { //点击获取手机号码按钮
    let that = this
    if(!that.data.checked) {
      wx.showToast({
        title: '请先阅读并同意《用户服务协议》及《隐私政策》',
        icon: 'none'
      })
      return
    }else {
      wx.checkSession({
        success: function (res) {
          var _data = {
            encrypted_data: e.detail.encryptedData,
            iv: e.detail.iv,
            session_key: wx.getStorageSync('session_key'),
          };
          
          //同意授权
          util.request(api.BindPhoneNumber, _data).then(res => {
            console.log(res);
            if (res.code === 0) {
              wx.setStorageSync('phone', res.data.phone_number),
                wx.redirectTo({
                  url: '/pages/auth/auth_idcard/auth_idcard',
                })
            }
          }).catch(err => console.log(err))
        }
      });
    }
  },
})